package mybatis.web;

import mybatis.pojo.Admin;
import mybatis.services.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @Author 汤舒青
 * @Version 1.0
 */

@WebServlet("/login")
public class LoginServlet  extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String loginName = request.getParameter("loginname");
        String pwd = request.getParameter("pwd");
        Admin admin = AdminService.ME.login(loginName, pwd);
        //if (loginName.startsWith("java78") && pwd.equals("123"))
        if (admin!=null){
            Cookie cookie = new Cookie("loginname", loginName);
            cookie.setMaxAge(86400*7);
            response.addCookie(cookie);
            HttpSession session=request.getSession();
            session.setAttribute("USER_LOGIN", loginName);
//        session.setAttribute("upass", pwd);
            response.sendRedirect("welcome.jsp");
        } else {
            request.setAttribute("msg", "用户名或者密码错误");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }
}
